/**
 * AUTHOR Creat
 */
package com.gcb.thread.pro2cus;

/**
 * @author Creat
 *
 */
public class ProductBuffer {

	private Product[] proBox = new Product[5];
	private int index = 0;
	
	public ProductBuffer(){
		super();
	}
	
	/**
	 * 生产者生成数据
	 * @param pro void
	 * @author Creat at 2011-11-7
	 */
	public synchronized void push(Product pro){
		while(index == proBox.length){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.notifyAll();
		proBox[index] = pro;
		index++;
	}
	
	/**
	 * 消费者消费数据
	 * @return Product
	 * @author Creat at 2011-11-7
	 */
	public synchronized Product pop(){
		while(index == 0){
			try {
				this.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		this.notifyAll();
		index--;
		return proBox[index];
	}
}
